package com.morningstar.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysMessageTemplate;
import com.morningstar.system.service.SysMessageTemplateService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import static com.morningstar.common.constants.permission.PermissionConstants.*;

/**
 * 消息模板接口
 *
 * @author Admin
 */
@Slf4j
@RestController
@RequestMapping("/system/message/template")
public class SysMessageTemplateController {

    private static final String PERMISSION = "system.message.template";
    private static final String PERMISSION_LIST = PERMISSION + LIST;
    private static final String PERMISSION_DETAIL = PERMISSION + DETAIL;
    private static final String PERMISSION_UPDATE = PERMISSION + UPDATE;
    private static final String PERMISSION_ADD = PERMISSION + ADD;
    private static final String PERMISSION_DELETE = PERMISSION + DELETE;

    @Resource
    private SysMessageTemplateService messageTemplateService;

    /**
     * 消息模板列表
     *
     * @param commonQuery 通用查询类
     * @return 列表
     */
    @PostMapping("/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<IPage<SysMessageTemplate>> list(@RequestBody CommonQuery<SysMessageTemplate, SysMessageTemplate> commonQuery) {
        SysMessageTemplate query = commonQuery.getQuery();
        LambdaQueryWrapper<SysMessageTemplate> queryWrapper = new LambdaQueryWrapper<>();
        if (query != null) {
            queryWrapper.like(StringUtils.isNotBlank(query.getTemplateName()), SysMessageTemplate::getTemplateName, query.getTemplateName());
            queryWrapper.like(StringUtils.isNotBlank(query.getTemplateKey()), SysMessageTemplate::getTemplateKey, query.getTemplateKey());
        }
        queryWrapper.orderByDesc(SysMessageTemplate::getCreateTime);
        queryWrapper.orderByDesc(SysMessageTemplate::getTemplateId);
        return Result.successData(messageTemplateService.page(commonQuery.getPage(), queryWrapper));
    }

    /**
     * 新增消息模板
     *
     * @param template 消息模板实体类
     * @return 基本结果类
     */
    @PostMapping("/add")
    @SaCheckPermission(PERMISSION_ADD)
    public Result<Object> add(@RequestBody SysMessageTemplate template) {
        ServiceCheckResult serviceCheckResult = messageTemplateService.checkAddAndUpdate(template);
        if (serviceCheckResult.isResult()) {
            return Result.fail(serviceCheckResult.getMessage());
        }
        messageTemplateService.save(template);
        return Result.successNoData();
    }

    /**
     * 修改消息模板
     *
     * @param template 消息模板实体类
     * @return 基本结果类
     */
    @PostMapping("/update")
    @SaCheckPermission(PERMISSION_UPDATE)
    public Result<Object> update(@RequestBody SysMessageTemplate template) {
        ServiceCheckResult serviceCheckResult = messageTemplateService.checkAddAndUpdate(template);
        if (serviceCheckResult.isResult()) {
            return Result.fail(serviceCheckResult.getMessage());
        }
        messageTemplateService.updateById(template);
        return Result.successNoData();
    }

    /**
     * 删除消息模板
     *
     * @param templateId 消息模板id
     * @return 基本结果类
     */
    @GetMapping("/delete")
    @SaCheckPermission(PERMISSION_DELETE)
    public Result<Object> delete(@RequestParam("templateId") Long templateId) {
        messageTemplateService.removeById(templateId);
        return Result.successNoData();
    }

    /**
     * 消息模板详情
     *
     * @param templateId 消息模板id
     * @return 消息模板实体类
     */
    @GetMapping("/detail")
    @SaCheckPermission(PERMISSION_DETAIL)
    public Result<SysMessageTemplate> detail(@RequestParam("templateId") Long templateId) {
        return Result.successData(messageTemplateService.getById(templateId));
    }
}
